package com.bdqn.controller;

import com.bdqn.pojo.Dept;
import com.bdqn.service.impl.deptServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 提供restfull请求
@RestController
public class deptController {

    @Autowired
    private deptServiceimpl deptS;

    //获取一些配置的信息，得到具体的微服务！
    @Autowired
    private DiscoveryClient client;

    @PostMapping("/dept/adddept")
    private String adddept(@RequestBody  Dept dept) {
        int i = deptS.adddept(dept);
        return i>0?"成功":"失败";
    }

    @RequestMapping("/dept/findDeptAll")
    private List<Dept> findDeptAll(){
        return deptS.findDeptAll();
    }

    @GetMapping("/dept/getDeptByid/{id}")
    Dept getDeptByid(@PathVariable  Integer id){
        return deptS.getDeptByid(id);
    }


    //注册进来的微服务~，获取一些消息~
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:" + services);
        //得到一个具体的微服务信息,通过具体的微服务id，applicaioinName；
        List<ServiceInstance> instances = client.getInstances("SPRING_CLOUD_PROVIDER-DEPT-8001");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" + // 主机名称
                            instance.getPort() + "\t" + // 端口号
                            instance.getUri() + "\t" + // uri
                            instance.getServiceId() // 服务id
            );
        }
        return this.client;
    }

}
